package NewCreateThreadMethod;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 李泽伟
 */
public class Main {
    public static void main(String[] args) {
        //3.创建Callable实现对象
        MyThread myThread = new MyThread();
        //4.将Callable对象传入FutureTask构造器，创建FutureTask对象
        FutureTask futureTask = new FutureTask(myThread);
        //5.启动线程
        new Thread(futureTask).start();
        //6.通过get()方法获取线程call()方法的返回值
        try {
            Object sum = futureTask.get();
            System.out.println(sum);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
/**
 * 1.创建一个Callable类
 */
class MyThread implements Callable{
    private int sum = 0;
    /**
     * 2.重写call方法，写入操作声明
     */
    @Override
    public Object call() throws Exception {
        for (int i = 0;i<=100;i++){
            sum += i;
            System.out.println(i);
        }
        return sum;
    }
}